package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.commonutils.result.R;
import com.atguigu.guli.service.edu.entity.form.CourseInfoForm;
import com.atguigu.guli.service.edu.entity.vo.CoursePublishVo;
import com.atguigu.guli.service.edu.entity.vo.CourseQueryVo;
import com.atguigu.guli.service.edu.entity.vo.CourseVo;
import com.atguigu.guli.service.edu.service.CourseService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author wangyang
 * @since 2021-05-26
 */
@Api(tags = "课程管理")
@RestController
@CrossOrigin
@RequestMapping("/admin/edu/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @ApiOperation("新增课程")
    @PostMapping("/save-course-info")
    public R saveCourseInfo(
            @ApiParam(value = "课程信息",required = true)
            @RequestBody CourseInfoForm courseInfoForm
            ) {
       String courseId = courseService.saveCourseInfo(courseInfoForm);
        return R.ok().data("courseId",courseId).message("添加成功");
    }

    @ApiOperation("根据Id查询课程")
    @GetMapping("/course-info/{id}")
    public R getById(
           @ApiParam(value = "课程Id",required = true)
           @PathVariable String id
    ){
        CourseInfoForm courseInfoForm = courseService.getCourseById(id);
        if (courseInfoForm != null) {
            return R.ok().data("item",courseInfoForm);
        } else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("更新课程")
    @PutMapping("/update-course-info")
    public R updateCourseInfoById(
            @ApiParam(value = "课程信息实体类",required = true)
            @RequestBody CourseInfoForm courseInfoForm
    ) {
        courseService.updateCourseInfoById(courseInfoForm);
        return R.ok().message("更新成功");
    }

    @ApiOperation("分页查询课程")
    @GetMapping("/findPage/{currentPage}/{pageSize}")
    public R findPage(
            @ApiParam(value = "当前页",required = true)
            @PathVariable long currentPage,
            @ApiParam(value = "页面大小",required = true)
            @PathVariable long pageSize,
            @ApiParam(value = "查询条件",required = false) CourseQueryVo courseQueryVo
            ) {
        Page page = (Page) courseService.selectPage(currentPage, pageSize, courseQueryVo);
        List list = page.getRecords();
        long total = page.getTotal();
        return R.ok().data("rows",list).data("total",total);
    }

    @ApiOperation("根据id删除课程表单")
    @DeleteMapping("/remove/{id}")
    public R removeCourseById(
            @ApiParam(value = "课程Id",required = true)
            @PathVariable String id
    ) {
        courseService.removeCoverById(id);
        boolean result = courseService.removeCourseById(id);
        if (result) {
            return R.ok().message("删除成功！");
        }else {
            return  R.error().message("删除失败！");
        }
    }

    @ApiOperation("根据ID获取课程发布信息")
    @GetMapping("/get-publish-course/{id}")
    public R getPublishCourseById(
            @ApiParam(value = "课程Id",required = true)
            @PathVariable String id
    ) {
        CoursePublishVo coursePublishVo = courseService.getPublishCourseById(id);
        if (coursePublishVo != null) {
            return R.ok().data("item",coursePublishVo);
        } else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("根据ID发布课程")
    @PutMapping("/publish-course/{id}")
    public R publishCourseById(
            @ApiParam(value = "课程Id",required = true)
            @PathVariable String id
    ) {
        boolean result = courseService.publishCourseById(id);
        if (result) {
            return R.ok().message("发布成功！");
        }else {
            return  R.error().message("发布失败！");
        }
    }

}

